package com.kum.server;

import com.github.monkeywie.proxyee.server.HttpProxyServer;
import com.github.monkeywie.proxyee.server.HttpProxyServerConfig;
import com.github.monkeywie.proxyee.server.accept.HttpProxyAcceptHandler;
import com.kum.utils.SpringUtils;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.HttpRequest;
import org.springframework.stereotype.Component;

import java.net.InetSocketAddress;

/**
 * @version V1.0
 * @Package PACKAGE_NAME
 * @auhter 枯木Kum
 * @date 2021/6/15-9:36 AM
 * <p>...</p>
 */

@Component
public class ProxyServerBootStrap {


    public static void start() {
        final HttpProxyServerConfig config = new HttpProxyServerConfig();
        config.setHandleSsl(true);

        config.setHttpProxyAcceptHandler(new HttpProxyAcceptHandler() {
            public boolean onAccept(HttpRequest request, Channel clientChannel) {
                InetSocketAddress inetSocketAddress = (InetSocketAddress) clientChannel.localAddress();
                String ip = inetSocketAddress.getHostString();
                request.setUri(request.uri() + "{{" + ip + "}}");
                return true;
            }

            public void onClose(Channel clientChannel) {

            }
        });


        new HttpProxyServer()
                .serverConfig(config)
                .proxyInterceptInitializer(SpringUtils.getBean(ServerInterceptInitializer.class))
                .start(10086);

    }


}
